package com.blacktech.dbu.meta.controller;

import com.blacktech.dbu.meta.dto.CreateFieldLibraryRequest;
import com.blacktech.dbu.meta.dto.FieldLibrarySearchRequest;
import com.blacktech.dbu.meta.dto.UpdateFieldLibraryRequest;
import com.blacktech.dbu.meta.entity.DbuFieldLibrary;
import com.blacktech.dbu.meta.service.FieldLibraryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字段库管理控制器
 *
 * @author Yanyunsong
 */
@RestController
@RequestMapping("/api/metadata/field-library")
@Tag(name = "字段库管理", description = "字段库管理相关接口，包含复杂搜索和翻译逻辑")
public class FieldLibraryController {

    @Autowired
    private FieldLibraryService fieldLibraryService;

    @PostMapping
    @Operation(summary = "创建字段库记录", description = "创建新的字段库记录")
    public DbuFieldLibrary createField(@Valid @RequestBody CreateFieldLibraryRequest request) {
        return fieldLibraryService.createField(request);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新字段库记录", description = "更新指定的字段库记录")
    public DbuFieldLibrary updateField(
            @Parameter(description = "字段库记录ID") @PathVariable Long id,
            @Valid @RequestBody UpdateFieldLibraryRequest request) {

        return fieldLibraryService.updateField(id, request);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除字段库记录", description = "删除指定的字段库记录")
    public void deleteField(@Parameter(description = "字段库记录ID") @PathVariable Long id) {
        fieldLibraryService.deleteField(id);
    }


    @PostMapping("/search")
    @Operation(summary = "搜索字段库记录", description = "根据条件搜索字段库记录")
    public Page<DbuFieldLibrary> searchFields(@Valid @RequestBody FieldLibrarySearchRequest request) {
        return fieldLibraryService.searchFields(request);
    }


    @GetMapping("/product/{productId}")
    @Operation(summary = "获取产品所有字段", description = "根据产品ID获取所有启用的字段列表")
    public List<DbuFieldLibrary> getAllFieldsByProduct(
            @Parameter(description = "产品ID") @PathVariable Long productId) {

        return fieldLibraryService.getAllFieldsByProduct(productId);
    }

    @GetMapping("/translate")
    @Operation(summary = "翻译字段名称", description = "将中文字段名翻译为英文建议")
    public String[] translateFieldName(
            @Parameter(description = "中文字段名称", required = true) @RequestParam String chineseName) {

        return fieldLibraryService.translateFieldName(chineseName);
    }
}